--锈枝依旧
local xiuzhiyijiu = fk.CreateSkill {
    name = "mcdh_xiuzhiyijiu",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

xiuzhiyijiu:addEffect(fk.AfterCardsMove,{
    can_trigger = function(self, event, target, player, data)
        if  player:hasSkill(self) and not player:isKongcheng() and  player:usedSkillTimes(self.name,Player.HistoryRound) == 0 then
            if event == fk.AfterCardsMove then
                for _, move in ipairs(data) do
                    if move.from then
                        local from = move.from
                        if from:isKongcheng()  then
                            for _, info in ipairs(move.moveInfo) do
                                if info.fromArea == Card.PlayerHand then
                                    return true
                                end
                            end
                        end
                    end
                end
            else
                return true
            end
        end
    end,
    on_trigger = function(self, event, target, player, data)
        local room = player.room
            local targets = {}
            for _, move in ipairs(data) do
                if move.from then
                    local from = move.from
                    if from:isKongcheng()  then
                        for _, info in ipairs(move.moveInfo) do
                            if info.fromArea == Card.PlayerHand then
                                table.insertIfNeed(targets, from)
                            end
                        end
                    end
                end
            end
            room:sortByAction(targets)
            for _, p in ipairs(targets) do
                local to = p
                --空城或者技能失效就结束询问
                if player:isKongcheng() or not player:hasSkill(self) or player:usedSkillTimes(self.name,Player.HistoryRound) > 0 then break end
                if not to.dead then
                    self:doCost(event, to, player, nil)
                end
            end
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local all_choices = {"peach","ex_nihilo","cancel"}
        local choices = all_choices

        local card = Fk:cloneCard("peach")
        if player:isProhibited(target, card) or not target:isWounded() then
            table.removeOne(choices,"peach")
        end
        card = Fk:cloneCard("ex_nihilo")
        if player:isProhibited(target, card) then
            table.removeOne(choices,"ex_nihilo")
        end
        if #choices ~= 1 then
            local choice = room:askForChoice(player,choices,self.name,"#mcdh_xiuzhiyijiu-use:"..target.id,nil,all_choices)
            if choice ~= "cancel" then
                self.cost_data = choice
                return true
            end
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local toPut = player:getCardIds("h")
        if #toPut > 1 then
            toPut = room:askForGuanxing(player, toPut, { #toPut, #toPut }, { 0, 0 }, self.name, true).top
            toPut = table.reverse(toPut)
        end
        room:moveCardTo(toPut, Card.DrawPile, nil, fk.ReasonPut, self.name, nil, true)
        local name  = self.cost_data
        local card = Fk:cloneCard(name)
        local targets = {target.id}
        -- on_cost 中已经进行了合法性检测此处直接使用就行
        local use = {
            from = player.id,
            tos = table.map(targets, function(p) return {p} end),
            card = card,
        }
        room:useCard(use)
    end
})

xiuzhiyijiu:addEffect(fk.EnterDying,{
    can_trigger = function(self, event, target, player, data)
        if  player:hasSkill(self) and not player:isKongcheng() and  player:usedSkillTimes(self.name,Player.HistoryRound) == 0 then
                return true
        end
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local all_choices = {"peach","ex_nihilo","cancel"}
        local choices = all_choices

        local card = Fk:cloneCard("peach")
        if player:isProhibited(target, card) or not target:isWounded() then
            table.removeOne(choices,"peach")
        end
        card = Fk:cloneCard("ex_nihilo")
        if player:isProhibited(target, card) then
            table.removeOne(choices,"ex_nihilo")
        end
        if #choices ~= 1 then
            local choice = room:askForChoice(player,choices,self.name,"#mcdh_xiuzhiyijiu-use:"..target.id,nil,all_choices)
            if choice ~= "cancel" then
                self.cost_data = choice
                return true
            end
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local toPut = player:getCardIds("h")
        if #toPut > 1 then
            toPut = room:askForGuanxing(player, toPut, { #toPut, #toPut }, { 0, 0 }, self.name, true).top
            toPut = table.reverse(toPut)
        end
        room:moveCardTo(toPut, Card.DrawPile, nil, fk.ReasonPut, self.name, nil, true)
        local name  = self.cost_data
        local card = Fk:cloneCard(name)
        local targets = {target}
        local use = {
            from = player,
            tos = targets,
            card = card,
        }
        room:useCard(use)
    end
})

Fk:loadTranslationTable {
    ["mcdh_xiuzhiyijiu"] = "锈枝依旧",
    [":mcdh_xiuzhiyijiu"] = "每轮限一次，当一名角色进入濒死时或失去最后的手牌后，你可以将所有手牌置于牌堆顶并视为对其使用【无中生有】或【桃】。",
    ["#mcdh_xiuzhiyijiu-use"] = "锈枝依旧:将所有手牌置于牌堆顶并视为对 %src 使用【无中生有】或【桃】。",
}

return xiuzhiyijiu